package cn.txun.csmall.product.controller;

import cn.txun.csmall.commons.web.JsonResult;
import cn.txun.csmall.product.pojo.param.AlbumAddNewParam;
import cn.txun.csmall.product.pojo.param.AlbumUpdateInfoParam;
import cn.txun.csmall.product.pojo.vo.AlbumListItemVO;
import cn.txun.csmall.product.pojo.vo.AlbumUpdateItemVO;
import cn.txun.csmall.commons.pojo.vo.PageData;
import cn.txun.csmall.commons.security.LoginPrincipal;
import cn.txun.csmall.product.service.IAlbumService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

/**
 * 处理相册相关请求的控制器类
 *
 * @author java@txun.cn
 * @version 0.0.1
 */
@RestController
@RequestMapping("/albums")
@Api(tags = "04.相册管理模块")
@Slf4j
@Validated
public class AlbumController {
    @Autowired
    //@Resource
    private IAlbumService albumService;


    //http://localhost:9180/album/add-new?name=TestName001&description=testDescription&sort=99
    //@RequestMapping(value = "/add-new",method =RequestMethod.POST)
    @PostMapping("/add-new")
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Validated AlbumAddNewParam albumAddNewParam , @ApiIgnore  HttpSession session) {
        log.debug("开始处理【添加相册】的请求，参数为{}，",albumAddNewParam);
        albumService.addNew(albumAddNewParam);
        log.debug("处理【添加相册】的请求，完成！");
        return JsonResult.ok();
    }
    //localhost:9180/albums/9527/delete
    //localhost:9180/albums/aaa/delete  404
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除相册")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(value = "相册ID",name = "id",required = true,dataType = "long")
    public JsonResult delete(@Range(min = 1,message = "根据相册ID删除失败，必须提交合法的ID值")
                                    @PathVariable  Long id){
        log.debug("开始处理【删除相册】的业务，参数：{}",id);
        albumService.delete(id);
        return JsonResult.ok();
    }

    //localhost:9180/album/delete
    @PostMapping("/update")
    @ApiOperation("修改相册")
    @PreAuthorize("hasAuthority('/pms/album/update')")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "相册ID", dataType = "long")
    })
    public JsonResult update(@Range(min = 1,message = "根据相册ID修改相册失败，必须提交合法的ID值")
                             @RequestParam Long id, @Validated AlbumUpdateInfoParam albumUpdateInfoParam,
                             @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){


        log.debug("开始处理【根据id修改相册详情】的业务，参数id：{},albumUpdateInfoParam:{}",id,albumUpdateInfoParam);
        log.debug("LoginPrincipal.id:{}",loginPrincipal.getId());
        log.debug("LoginPrincipal.username:{}",loginPrincipal.getUsername());
        albumService.updateInfoById(id,albumUpdateInfoParam);
        return JsonResult.ok();
    }

    //localhost:9180/albums/{id}
    @GetMapping("/{id:[0-9]+]}")
    @ApiOperation("根据ID查询相册详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(value = "相册ID",name = "id",required = true,dataType = "long")
    public JsonResult getInfoById(@Range(min = 1,message = "根据相册ID查询详情失败，必须提交合法的ID值")
                             @PathVariable Long id){
        log.debug("开始处理【根据id查询相册详情】的业务，参数：{}",id);
        AlbumUpdateItemVO queryResult = albumService.getAlbumUpdateItemVOById(id);
        return JsonResult.ok(queryResult);
    }

    //localhost:9180/albums/{1}/page
    @GetMapping("/{pageNum:[0-9]+}/page")
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码", dataType = "integer")
    })
    public JsonResult list(@Range(min=1,message = "查询相册列表失败,请提供正确的页码值")
                          @PathVariable  Integer pageNum){
        log.debug("开始处理【查询相册列表】的请求，参数为{}，",pageNum);
        if(pageNum==null || pageNum<1){
            pageNum=1;
        }
        PageData<AlbumListItemVO> pageData = albumService.list(pageNum);
        log.debug("处理【查询相册列表】的请求，完成！数据：{}",pageData);;
        return JsonResult.ok(pageData);
    }

}
